package com.lagoue.spark

import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{Dataset, SparkSession, functions}

/**
 * @author: yehw
 * @date: 2020/10/23 23:17
 * @description: sql
 */
case class Dates(id:Int,time: String)

object homework6 {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder().appName("Demo1").master("local[*]").getOrCreate()
    val sc = spark.sparkContext
    sc.setLogLevel("warn")
    import spark.implicits._
    val arr = Array("1 2019-03-04 2020-02-03", "2 2020-04-05 2020-08-04", "3 2019-10-09 2020-06-11")
    val rdd: RDD[Dates] = sc.makeRDD(arr).map { line =>
      val fields: Array[String] = line.split("\\s+")
      Dates(fields(0).toInt,fields(1)+","+fields(2))
    }
    val ds: Dataset[Dates] = spark.createDataset(rdd)
    val result = ds.withColumn("time", functions.explode(functions.split(functions.col("time"), ",")))
    result.show(false)
    result.sort("time").createOrReplaceTempView("t1")
    spark.sql("select  time as time1 ,nvl(lead(time) over(order by time),time) time2 from t1").show(false)
    spark.close()
  }
}
